package com.igeek.health.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.health.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.Set;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author fengqq
 * @since 2024-08-26
 */
public interface UserMapper extends BaseMapper<User> {

    //查询 管理员信息 包含 一对多管理 Set<Role>
    //${ew.customSqlSegment}  拼接在自定义sql语句的where后面的   ew代表的是QueryWrapper对象
    //${ew.sqlSet}  拼接在自定义sql语句的update set后面的
    @Select("select * from user ${ew.customSqlSegment}")
    @Results({
            @Result(id = true,column = "id",property = "id"), //主键字段的映射 查询列和哪个属性进行关联
            @Result(
                    property = "roles",
                    column = "id",//第一次查询的结果字段 作为 关联查询的条件
                    javaType = Set.class,
                    many = @Many(select="com.igeek.health.mapper.RoleMapper.selectRolesByUserId")) //一对多关联
    })
    Page<User> selectUserRolesPage(Page<User> page, @Param("ew") LambdaQueryWrapper<User> queryWrapper);

    //根据username查询用户信息
    @Select("select * from user where username=#{username}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(
                    property = "roles",javaType =Set.class ,
                    column = "id",
                    many = @Many(select = "com.igeek.health.mapper.RoleMapper.selectRolesByUserId"))
    })
    public User selectUserByName(String username);
}
